import { createRouter, createWebHashHistory } from "vue-router";
import nProgress from "nprogress";
// import { ref } from 'vue';
// import { getItems } from '@/Utils/storage';

const routes = [
	{
		path: "/",
		name: "layout",
		component: () => import("@/views/layout/index.vue"),
		children: [
			{
				name: "home",
				path: "/",
				meta: { name: ["首页"] },
				component: () => import("@/views/content/home/home.vue"),
			},
		],
	},
	{
		path: "/login",
		name: "Login",
		component: () => import("../views/login/index.vue"),
	},
	{
		path: "/:pathMatch(.*)/",
		component: () => import("@/views/error/index.vue"),
	},
];

const router = createRouter({
	history: createWebHashHistory(),
	routes,
});

// 路由信息
const routerInfo = [
	{
		name: "业务中心",
		path: "/bus",
		children: [
			{
				name: "学员档案",
				path: "/studentRecord",
			},
			{
				name: "跟进记录",
				path: "/trackLog",
			},
			{
				name: "试听记录",
				path: "/tryClassLog",
			},
			{
				name: "营销活动",
				path: "/promoteMgr",
			},
			{
				name: "预警提示",
				path: "/warningMgr",
			},
		],
	},
	{
		name: "教务中心",
		path: "/educational",
		children: [
			{
				name: "学员管理",
				path: "/studentMgr",
			},
			{
				name: "班级管理",
				path: "/classMgr",
			},
			{
				name: "课表管理",
				path: "/timetableMgr",
			},
			{
				name: "课程管理",
				path: "/courseMgr",
			},
			{
				name: "物品/费用",
				path: "/toll",
			},
			{
				name: "在线商城",
				path: "/mallMgr",
			},
			{
				name: "老师管理",
				path: "/teacher",
			},
			{
				name: "上课记录",
				path: "/classRecordMgr",
			},
			{
				name: "学员考勤",
				path: "/faceCheckMgr",
			},
		],
	},
	{
		name: "家校互动",
		path: "/interaction",
		children: [
			{
				name: "课后点评",
				path: "/classEvaluate",
			},
			{
				name: "课后作业",
				path: "/homework",
			},
			{
				name: "成长档案",
				path: "/growthRecord",
			},
			{
				name: "电子相册",
				path: "/album",
			},
			{
				name: "成绩单",
				path: "/achievement",
			},
			{
				name: "通知管理",
				path: "/notice",
			},
		],
	},
	{
		name: "营销中心",
		path: "/marketingMgr",
		children: [
			{
				name: "优惠券",
				path: "/couponsMgr",
			},
			{
				name: "礼品中心",
				path: "/giftMgr",
			},
			{
				name: "小禾招生",
				path: "/activity",
			},
			{
				name: "微官网",
				path: "/microWebMgr",
			},
		],
	},
	{
		name: "财务中心",
		path: "/finance",
		children: [
			{
				name: "订单管理",
				path: "/orderMgr",
			},
			{
				name: "充值管理",
				path: "/rechargeManage",
			},
			{
				name: "收支明细",
				path: "/inExMgr",
			},
			{
				name: "课消记录",
				path: "/courseChangeLog",
			},
			{
				name: "工资结算",
				path: "/salaryMgr",
			},
		],
	},
	{
		name: "数据中心",
		path: "/data",
		children: [
			{
				name: "经营汇总",
				path: "/tenantSta",
			},
			{
				name: "招生数据",
				path: "/students",
			},
			{
				name: "销售数据",
				path: "/sales",
			},
			{
				name: "教务数据",
				path: "/academic",
			},
			{
				name: "财务数据",
				path: "/financeData",
			},
		],
	},
	{
		name: "机构设置",
		path: "/tenantMgr",
		children: [
			{
				name: "系统设置",
				path: "/SystemSet",
			},
			{
				name: "机构展示",
				path: "/tenantSet",
			},
		],
	},
	{
		name: "增值服务",
		path: "/increment",
		children: [
			{
				name: "微信助手",
				path: "/WxMgr",
			},
			{
				name: "聚合支付",
				path: "/paySet",
			},
		],
	},
];

export function initRouter() {
	// 获取到当前的路由信息
	const routes = router.options.routes;
	const route = routes.find((item) => item.name == "layout");
	// console.log(routes, route);
	// 将当前登录人具有的权限页面添加到路由中
	routerInfo.forEach((item1) => {
		item1.children.forEach((item2) => {
			route.children.push({
				path: item1.path + item2.path,
				component: () => import(`@/views/content${item1.path + item2.path}.vue`),
				meta: { name: [item1.name, item2.name], path: item1.path + item2.path },
			});
		});
	});
	console.log(route);
	// 将修改之后的route添加进router让他生效
	router.addRoute(route);
}
initRouter();

router.beforeEach((to, from, next) => {
	nProgress.start();
	next();
});
router.afterEach(() => {
	nProgress.done();
});

export default router;
